// DatoNotat.cpp : Implementation of CDatoNotat
#include "stdafx.h"
#include "EBASdll.h"
#include "DatoNotat.h"

/////////////////////////////////////////////////////////////////////////////
// CDatoNotat

STDMETHODIMP CDatoNotat::InterfaceSupportsErrorInfo(REFIID riid)
{
	static const IID* arr[] = 
	{
		&IID_IDatoNotat
	};
	for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
	{
		if (InlineIsEqualGUID(*arr[i],riid))
			return S_OK;
	}
	return S_FALSE;
}

STDMETHODIMP CDatoNotat::get_GUID(BSTR *pVal)
{
	*pVal = m_guid.copy();

	return S_OK;
}

STDMETHODIMP CDatoNotat::put_GUID(BSTR newVal)
{
	m_guid = newVal;

	return S_OK;
}

STDMETHODIMP CDatoNotat::get_Dato(BSTR *pVal)
{
	*pVal = m_dato.copy();

	return S_OK;
}

STDMETHODIMP CDatoNotat::put_Dato(BSTR newVal)
{
	m_dato = newVal;

	return S_OK;
}

STDMETHODIMP CDatoNotat::get_Emne(BSTR *pVal)
{
	*pVal = m_emne.copy();

	return S_OK;
}

STDMETHODIMP CDatoNotat::put_Emne(BSTR newVal)
{
	m_emne = newVal;

	return S_OK;
}

STDMETHODIMP CDatoNotat::get_Notat(VARIANT *pVal)
{
	*pVal = m_notat.Detach();

	return S_OK;
}

STDMETHODIMP CDatoNotat::put_Notat(VARIANT newVal)
{
	m_notat = newVal;

	return S_OK;
}

STDMETHODIMP CDatoNotat::get_StuderendeGuid(BSTR *pVal)
{
	*pVal = m_studGuid.copy();

	return S_OK;
}

STDMETHODIMP CDatoNotat::put_StuderendeGuid(BSTR newVal)
{
	m_studGuid = newVal;

	return S_OK;
}

STDMETHODIMP CDatoNotat::SetInfo()
{
	_CommandPtr command;	

	const char sql[255] = "EBAS_SP_GEM_DATO_NOTAT";
	long lower = 0, upper = 0, diff = 0;

	try {
		if (m_emne.length() > 250)
			return E_FAIL;

		SafeArrayGetLBound(m_notat.parray, 1, &lower);
		SafeArrayGetUBound(m_notat.parray, 1, &upper);
		diff = upper - lower;

		command.CreateInstance(__uuidof(Command));		
		command->ActiveConnection = CDatabase::GetConnection();
		command->CommandText = sql;
		command->CommandType = adCmdStoredProc;
		command->Parameters->Append( command->CreateParameter("session", adVarChar, adParamInput, 255, gpSessionGuid) );
		command->Parameters->Append( command->CreateParameter("studGuid", adVarChar, adParamInput, 255, m_studGuid) );
		command->Parameters->Append( command->CreateParameter("dato", adVarChar, adParamInput, 255, m_dato) );
		command->Parameters->Append( command->CreateParameter("emne", adVarChar, adParamInput, 255, m_emne) );
		command->Parameters->Append( command->CreateParameter("filNavn", adVarChar, adParamInput, 255, m_filNavn) );
		
		m_recordset = command->Execute(NULL, NULL, adCmdStoredProc);
		if(m_recordset->ADO_EOF)
			return E_ACCESSDENIED;

		m_guid = (bstr_t)m_recordset->Fields->GetItem("GUID")->Value;
		m_dato = (bstr_t)m_recordset->Fields->GetItem("DATO")->Value;

		bstr_t binSql = "SELECT NOTAT_BINARY FROM EBAS_DATO_NOTAT WHERE GUID = '" + m_guid + "'";
		CDatabase::StoreBinary(binSql, "NOTAT_BINARY", m_notat);
	} catch (_com_error &e) {
		CErrorHandling::SetErrorMessage(e.ErrorMessage());
		return e.Error();
	}
	return S_OK;
}

STDMETHODIMP CDatoNotat::get_FilNavn(BSTR *pVal)
{
	*pVal = m_filNavn;

	return S_OK;
}

STDMETHODIMP CDatoNotat::put_FilNavn(BSTR newVal)
{
	m_filNavn = newVal;

	return S_OK;
}
